﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AEM.Data;
using AEM.Data.Enum;
using AEM.Common;
using AEM.Utils;

namespace AEM.Controls
{
    /// <summary>
    /// WaitingRatio.xaml 的交互逻辑
    /// </summary>
    public partial class WaitingRatio : UserControl
    {
        public WaitingRatio()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WaitingRatio_Loaded);
            this.btn_select.Click += new RoutedEventHandler(btn_select_Click);
            this.datefrom.ValueChanged += new EventHandler(datefrom_SelectedDateChanged);
            this.dateto.ValueChanged += new EventHandler(dateto_SelectedDateChanged);
            this.listWaitingRatio.SelectionChanged += new SelectionChangedEventHandler(listWaitingRatio_SelectionChanged);
            this.btn_reset.Click += new RoutedEventHandler(btn_reset_Click);
            this.liftNum.SelectionChanged += new SelectionChangedEventHandler(liftNum_SelectionChanged);
        }

        void liftNum_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.Model.LiftInfo r = (Data.Model.LiftInfo)this.liftNum.SelectedItem;
            if (null == r) return;
            this.liftLandings.Visibility = Visibility.Visible;

            List<KeyValuePair<string, int>> flist = new List<KeyValuePair<string, int>>();
            string[] floors = r.LiftActualFloor.Split(',');
            int len = floors.Length;
            for (int i = 1; i <= len; i++)
            {
                flist.Add(new KeyValuePair<string, int>(floors[i - 1], i));
            }
            this.liftLandings.ItemsSource = flist;
            this.liftLandings.Items.Refresh();
        }

        void btn_reset_Click(object sender, RoutedEventArgs e)
        {
            this.liftNum.SelectedItem = null;
            this.liftDirection.SelectedItem = null;
            this.liftLandings.SelectedItem = null;
            this.datefrom.Value = DateTime.Now;
            this.dateto.Value = DateTime.Now;
            this.liftLandings.Visibility = Visibility.Collapsed;
        }

        void WaitingRatio_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != Config.LiftInftList)
                this.liftNum.ItemsSource = from Data.Model.LiftInfo r in Config.LiftInftList
                                           where r.LiftType == (int)LiftType.电梯
                                           select r;
            this.liftNum.Items.Refresh();
            //this.liftNum.SelectedIndex = 0;

            DateTime s = DataLogHelper.GetEarliestTime();
            if (null != s)
            {
                this.datefrom.DisplayDateStart = s;
                this.dateto.DisplayDateStart = s;
            }
            else
            {
                this.datefrom.DisplayDateStart = DateTime.Now;
            }
            this.datefrom.DisplayDateEnd = DateTime.Now;
            this.dateto.DisplayDateEnd = DateTime.Now;

            List<KeyValuePair<string, int>> dlist = new List<KeyValuePair<string, int>>();
            dlist.Add(new KeyValuePair<string, int>(this.TryFindResource("UpCall") as string, (int)CallType.Up));
            dlist.Add(new KeyValuePair<string, int>(this.TryFindResource("DownCall") as string, (int)CallType.Down));
            this.liftDirection.ItemsSource = dlist;
            this.liftDirection.Items.Refresh();
            //if (null != Config.LiftInftList)
            //{
            //    var f = (from Data.Model.LiftInfo r in Config.LiftInftList
            //             where r.LiftType == (int)LiftType.电梯
            //             select r.LiftActualFloor).Max();

            //    List<KeyValuePair<string, int>> flist = new List<KeyValuePair<string, int>>();
            //    int len = f.Split(',').Length;
            //    for (int i = 1; i <= len; i++)
            //    {
            //        flist.Add(new KeyValuePair<string, int>(i.ToString(), i));
            //    }
            //    this.liftLandings.ItemsSource = flist;
            //    this.liftLandings.Items.Refresh();
            //}
            ResetSeries();


            this.waitingSeries.Title = this.TryFindResource("TimeUnit") as string;
        }

        private void ResetSeries()
        {
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            valueList.Add(new KeyValuePair<string, int>(this.TryFindResource("MaxWaiting") as string, 0));
            valueList.Add(new KeyValuePair<string, int>(this.TryFindResource("AvgWaiting") as string, (int)0));
            valueList.Add(new KeyValuePair<string, int>(this.TryFindResource("MinWaiting") as string, 0));
            this.waitingRatio.DataContext = valueList;
        }

        void dateto_SelectedDateChanged(object sender, EventArgs e)
        {
            this.datefrom.DisplayDateEnd = this.dateto.Value;
        }

        void datefrom_SelectedDateChanged(object sender, EventArgs e)
        {
            this.dateto.DisplayDateStart = this.datefrom.Value;
        }

        void btn_select_Click(object sender, RoutedEventArgs e)
        {
            this.btn_reset.IsEnabled = false;
            ResetSeries();
            this.msg.Text = "";
            if (null == this.datefrom.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateFrom") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            if (null == this.dateto.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateTo") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            DateTime s = this.datefrom.Value.Value;
            DateTime t = this.dateto.Value.Value;
            int d = -1;
            int l = -1;
            if (null != this.liftDirection.SelectedValue)
                d = (int)this.liftDirection.SelectedValue;
            if (null != this.liftLandings.SelectedValue)
                l = (int)this.liftLandings.SelectedValue;

            this.msg.Text = this.TryFindResource("Searching") as string;
            if (null != this.liftNum.SelectedItem)
            {
                Data.Model.LiftInfo r = (Data.Model.LiftInfo)this.liftNum.SelectedItem;

                ResultData rd = CreateItem(r, s, t, d, l);
                this.listWaitingRatio.ItemsSource = new List<ResultData>() { rd };
            }
            else
            {
                List<ResultData> list = new List<ResultData>();

                foreach (Data.Model.LiftInfo r in Config.LiftInftList)
                {
                    list.Add(CreateItem(r, s, t, d, l));
                }

                this.listWaitingRatio.ItemsSource = list;
            }

            this.listWaitingRatio.Items.Refresh();
            this.listWaitingRatio.SelectedIndex = 0;
            this.btn_reset.IsEnabled = true;
            this.msg.Text = this.TryFindResource("SearchOK") as string;
        }

        void listWaitingRatio_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshSeries(this.listWaitingRatio.SelectedItem as ResultData);
        }

        void RefreshSeries(ResultData rd)
        {
            if (null == rd) return;
            List<KeyValuePair<string, double>> valueList = new List<KeyValuePair<string, double>>();
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("MaxWaiting") as string, rd.MaxWaiting));
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("AvgWaiting") as string, rd.AvgWaiting));
            valueList.Add(new KeyValuePair<string, double>(this.TryFindResource("MinWaiting") as string, rd.MinWaiting));
            this.waitingRatio.DataContext = valueList;

            this.waitingRatio.LegendTitle = rd.LiftInfo.LiftSite;
        }

        private ResultData CreateItem(Data.Model.LiftInfo r, DateTime s, DateTime t, int d, int l)
        {
            try
            {
                double max = 0, min = 0, avg = 0;
                double x = DataLogHelper.GetWaitingTime(r, s, t, d, l, out max, out min, out avg);
                return new ResultData(r, max / 1000, avg / 1000, min / 1000);
            }
            catch
            {
                return null;
            }
        }
    }

    public class ResultData
    {
        public ResultData(Data.Model.LiftInfo l, double max, double avg, double min)
        {
            this.LiftInfo = l;
            this.ShortName = l.ShortName;
            this.MinWaiting = min;
            this.ShowMin = (new TimeSpan(0, 0, 0, min.toInt32())).ToString("g");
            this.AvgWaiting = avg;
            this.ShowAvg = (new TimeSpan(0, 0, 0, avg.toInt32())).ToString("g");
            this.MaxWaiting = max;
            this.ShowMax = (new TimeSpan(0, 0, 0, max.toInt32())).ToString("g");
        }
        public Data.Model.LiftInfo LiftInfo { get; set; }
        public string ShortName { get; set; }
        public double MinWaiting { get; set; }
        public string ShowMin { get; set; }
        public double AvgWaiting { get; set; }
        public string ShowAvg { get; set; }
        public double MaxWaiting { get; set; }
        public string ShowMax { get; set; }
    }
}
